import { legacy_createStore as createStore, applyMiddleware } from "redux";

// 中间件是做一些异步的操作
import thunk from "redux-thunk";
// devtool
import { composeWithDevTools } from "redux-devtools-extension";
// 导入模块


// 定义数据
const initState = {
    count: 0,
    name: '张三'
};
function countReducer(state = initState, action: any) {
    const { type, data } = action;
    switch (type) {
        case 'incNumber':
            return addCount(state, data)
        case 'updateName': 
            return { ...state, name: '李四'}
        default:
            return state;
    }
}

function addCount(state: any, data: any) {
    return {
        ...state,
        count: state.count + Number(data)
    }
}


const store = createStore(countReducer, composeWithDevTools(applyMiddleware(thunk)))

// 暴露 store
export default store